import { Component, Input, OnInit} from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { RedpackRecordService } from '@core/routes-services/redpack-record.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-trigger-record-list',
  templateUrl: './trigger-record-list.component.html',
})
export class TriggerRecordListComponent implements OnInit {

  @Input() customerId: any;
  @Input() triggerId: any;
  isLoading = false;
  @Input() keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  constructor(private redpackRecordService: RedpackRecordService,
              public message: NzMessageService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit(): void {

    this.initPage();
  }
  initPage() {
    this.isLoading = true;
    // 查定时发放器的发放明细
    this.page['_filter_eq_type'] = 100;
    if(this.customerId){
      this.page['_filter_eq_cusId'] = this.customerId;
    }
    if(this.triggerId){
      this.page['_filter_eq_relateId'] = this.triggerId;
    }
    this.redpackRecordService.pagination(this.page, this.keyword).then((res) => {
      console.log(res)
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

//是否在重复中，防止重复发放
  reSending: boolean = false;
  reSend = (entity) => {
    if (!this.reSending) {
      this.reSending = true;
      this.redpackRecordService.reSendCash(entity.id).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.message.success('补发成功');
          this.changePageNo(this.page.number);
        } else {
          this.message.warning(res.message);
        }
        this.reSending = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.reSending = false;
      });
    }
  };
}
